﻿using System;
using System.Collections.Generic;
using System.Data.OleDb;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Security.Permissions;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace VisionRoc.Tool
{
    /// <summary>
    /// 创建MDB文件及写入
    /// </summary>
    public class MdbAPI
    {
        string m_Path;
        public bool CreateTable(string _path)
        {
            string path = _path + "\\" + DateTime.Now.ToString("yyyyMMdd") + ".mdb";
            if (!File.Exists(path))
            {
                try
                {
                    string ModelPath = Application.StartupPath + "\\Solution\\MDB\\model.mdb";
                    File.Copy(ModelPath, path);
                }
                catch (System.Exception ex)
                {
                    TxtAPI.WriteLogTxt($"创建MDB文件异常:{ex.ToString()}");
                    return false;
                }
            }
            m_Path = "provider=microsoft.ACE.OLEDB.12.0;jet oledb:database password='';user id='admin';data source=" + path;
            return true;
        }

        public int ExecuteNonQuery(string sql, params OleDbParameter[] parameters)
        {
            try
            {
                using (OleDbConnection conn = new OleDbConnection(m_Path))
                {
                    conn.Open();

                    using (OleDbCommand cmd = conn.CreateCommand())
                    {
                        cmd.CommandText = sql;
                        cmd.Parameters.AddRange(parameters);
                        return cmd.ExecuteNonQuery();
                    }
                }

            }
            catch (Exception ex)
            {
                TxtAPI.WriteLogTxt($"ExecuteNonQuery函数异常:{ex.ToString()}");
                return -1;
            }
        }

        public void Insert_Values(ModelData m)
        {

            ExecuteNonQuery("insert into CKData(BARCODE,FDATE) values (@_BARCODE,@_FDATE)",
                            new OleDbParameter("@_BARCODE", m.barCode),
                            new OleDbParameter("@_FDATE", m.fDate)
                            //new OleDbParameter("@_result", m.result),
                            //new OleDbParameter("@_flag", m.flag),
                            //new OleDbParameter("@_errorCode", m.errorCode),
                            //new OleDbParameter("@_lineNo", m.lineNo),
                            //new OleDbParameter("@_model", m.model),
                            //new OleDbParameter("@_right2262", m.RIGHT_2262_2332),
                            //new OleDbParameter("@_right2262Res", m.RIGHT_2262_2332_RESULT),
                            //new OleDbParameter("@_left2262", m.LEFT_2262_2332),
                            //new OleDbParameter("@_left2262Res", m.LEFT_2262_2332_RESULT),
                            //new OleDbParameter("@_vehicleCode", m.vehicleCode),
                            //new OleDbParameter("@_labelSn", m.labelSn),
                            //new OleDbParameter("@_hostName", m.hostName),
                            //new OleDbParameter("@_deviceName", m.deviceName)
                            );
        }

    }

    public class ModelData
    {
        public string barCode;
        public string fDate;
    }

}
